import java.util.ArrayList;

/*
 * This filter determines wild point values based on the specification described in
 * System C section 2 - comparing both Pressure and Attitude
 * @author Team 10
 */
public class AttitudeWPFilter extends FilterFramework2
{
	public AttitudeWPFilter() 
	{
		// this filter is to have one input and one output
		super(1, 1);
	}

	/*
	 * This filter replaces wild point pressure values with extrapolated data.
	 * We assumed there was at least ONE valid pressure value in the list of all data entries
	 * from which to base an accurate extrapolation.
	 */
	public void run()
    {
		// previousPress stores the last satisfactory pressure value
		Double previousPress = null;

		// this buffer stores all the measurements with wild points awaiting normalization
		ArrayList<Measurement> buffer = new ArrayList<Measurement>();
					
		while (true)
		{
			try
			{
				// This measurement will aid with extracting and setting specific data within
				// an entire Data Frame
				Measurement measurement = new Measurement();
				
				// read bytes from Pipe 0
				measurement.readByteStream(this, 0);

				// grab the pressure and attitude values
				Double press = (Double)measurement.getValue(3);
				Double attitude = (Double)measurement.getValue(5);
				
				// if pressure and attitude are in wild point range (invalid)
				if(press > 65 && attitude > 10)
				{
					// we add them to the buffer for normalization
					buffer.add(measurement);
				}
				else // we have valid values
				{
					// if there are buffered measurements, we need to normalize them
					// using this new valid pressure value
					if(buffer.size() > 0)
					{
						// if there has not yet been a valid pressure value
						if(previousPress == null)
						{
							// we set the previous pressure equal to this pressure value
							// (the first valid value)
							previousPress = press;
						}
						
						// the normalized value is equal to the average of the last valid
						// and the next valid pressure values
						Double normalized = (previousPress+press)/2;
						
						//for each buffered measurement
						for(int i=0; i<buffer.size(); i++)
						{
							// normalize its pressure value
							buffer.get(i).setValue(3, normalized);
							buffer.get(i).writeByteStream(this, 0);
						}
						
						// clear the buffer
						buffer.clear();
					}
					
					// write the current measurement to the port
					measurement.writeByteStream(this, 0);
					
					// store the recent valid value as the next iteration's previous
					previousPress = press;
				}
			} // try

			catch (EndOfStreamException e)
			{
				// for each element awaiting normalization in the buffer,
				for(int i=0; i<buffer.size(); i++)
				{
					// set the pressure value to the previous valid pressure value
					buffer.get(i).setValue(3, previousPress);
					buffer.get(i).writeByteStream(this, 0);
				}
				break;

			} // catch

		} // while

   } // run
}
